from flask import Flask, request
from submodules.common.lib.log_ import logger
from handlers import user_tmp, court, city, user, old_house, neiborhood
from lib.http import Request
from flask_cors import CORS

app = Flask(__name__)
CORS(app, resources=r"/*")


def init_request(request_):
    req = Request()
    req.body = request_.get_json()
    req.data = request_.get_data()
    logger.info(f"body:{req.body}")
    logger.info(f"data:{req.data}")
    return req


@app.route("/hi/api/user_tmp/create")
def user_tmp_create():
    '''
    {
        "items": [{
            "id": "",
            "court_id": "94911711344",
            "category": 3,
            "discount_type": 0,
            "object_type": 0,
            "object_id": "94911711344",
            "content": "100",
            "desc": "1111",
            "start_time": "1632758400",
            "end_time": "1632931199",
            "extra_info": "{\"xfl_cost\":\"1\"}"
        }]
    }
    {
        "age": "18",
        "f_name": "han",
    }
    :return:
    {
        "status": 1008,
        "message": "保存失败！该楼盘已有上架中的补贴优惠(优惠id 7002564196994924588)，如需添加新的补贴优惠，请先下架原有优惠。",
        "data": null
    }
    '''
    req = init_request(request)
    return user_tmp.user_tmp_create(req)


@app.route("/hi/api/user_tmp/list")
def user_tmp_list():
    '''
    {
        "ids": ["6992048669889413164"]
    }
    {
        "court_ids": ["", ""],
        "city_ids": [""],
        "categories": [3],
        "discount_statuses": [1],
        "page": 1,
        "size": 10
    }
    :return:
    {
        "status": 0,
        "message": "success",
        "data": {
            "pagination": {
                "page": 1,
                "size": 10,
                "total": 15,
                "has_more": true
            },
            "items": [{
            }]
        }
    }
    '''
    return "123"


@app.route("/hi/api/user_tmp/modify")
def user_tmp_modify():
    '''
    {
        "items": [{
            "id": "6992048669889413164",
            "court_id": "72554930258780",
            "category": 3,
            "discount_type": 0,
            "object_type": 0,
            "object_id": "72554930258780",
            "content": "1000000",
            "desc": "这是购房大补贴啊，快快来",
            "start_time": "1628352000",
            "end_time": "1628352000",
            "extra_info": "{\"kfs_cost\":\"300000\",\"xfl_cost\":\"700000\"}"
        }]
    }
    :return:
    {
        "status": 1001,
        "message": "参数错误"
    }
    '''
    return "456"


@app.route("/hi/api/user_tmp/update_status")
def user_tmp_update_status():
    '''
    {
        "ids": ["6993155218569445420"],
        "action": 2
    }
    :return:
    {
        "status": 0,
        "message": "success"
    }
    '''
    return "<p>Hello, World!</p>"


# 新房
@app.route("/hi/api/court/create", methods=["POST"])
def court_create():
    req = init_request(request)
    return court.court_create(req)


@app.route("/hi/api/court/update_stauts", methods=["POST"])
def court_update_status():
    req = init_request(request)
    return court.court_update_status(req)


@app.route("/hi/api/court/modify", methods=["POST"])
def court_modify():
    req = init_request(request)
    return court.court_modify(req)


@app.route("/hi/api/court/list", methods=["POST"])
def court_list():
    req = init_request(request)
    return court.court_list(req)


# 二手房
@app.route("/hi/api/old_house/create", methods=["POST"])
def old_house_create():
    req = init_request(request)
    return old_house.old_house_create(req)


@app.route("/hi/api/old_house/update_stauts", methods=["POST"])
def old_house_update_status():
    req = init_request(request)
    return old_house.old_house_update_status(req)


@app.route("/hi/api/old_house/modify", methods=["POST"])
def old_house_modify():
    req = init_request(request)
    return old_house.old_house_modify(req)


@app.route("/hi/api/old_house/list", methods=["POST"])
def old_house_list():
    req = init_request(request)
    return old_house.old_house_list(req)


# 小区
@app.route("/hi/api/neiborhood/create", methods=["POST"])
def neiborhood_create():
    req = init_request(request)
    return neiborhood.neiborhood_create(req)


@app.route("/hi/api/neiborhood/update_stauts", methods=["POST"])
def neiborhood_update_status():
    req = init_request(request)
    return neiborhood.neiborhood_update_status(req)


@app.route("/hi/api/neiborhood/modify", methods=["POST"])
def neiborhood_modify():
    req = init_request(request)
    return neiborhood.neiborhood_modify(req)


@app.route("/hi/api/neiborhood/list", methods=["POST"])
def neiborhood_list():
    req = init_request(request)
    return neiborhood.neiborhood_list(req)


# 获取城市列表
@app.route("/hi/api/city/list", methods=["POST"])
def city_list():
    req = init_request(request)
    return city.city_list(req)


# 登录
@app.route("/hi/api/user/login", methods=["POST"])
def user_login():
    req = init_request(request)
    return user.user_login(req)


# 登录
@app.route("/hi/api/user/logout", methods=["POST"])
def user_logout():
    req = init_request(request)
    return user.user_logout(req)


# 获取用户信息
@app.route("/hi/api/user/info", methods=["POST"])
def user_info():
    req = init_request(request)
    return user.user_info(req)


# 用户
@app.route("/hi/api/user/create", methods=["POST"])
def user_create():
    req = init_request(request)
    return user.user_create(req)


@app.route("/hi/api/user/update_stauts", methods=["POST"])
def user_update_status():
    req = init_request(request)
    return user.user_update_status(req)


@app.route("/hi/api/user/modify", methods=["POST"])
def user_modify():
    req = init_request(request)
    return user.user_modify(req)


@app.route("/hi/api/user/list", methods=["POST"])
def user_list():
    req = init_request(request)
    return user.user_list(req)


app.run()
